// JavaScript Document

var numberOfMenuLevels = 2;

var keyboardFocus = null;

var isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;

/*
makeFrame Function inserts 1 iframe per number of dropdowns in menu system.
The number of dropdown levels is specified in the var numberOfMenuLevels (above).
*/
makeFrame = function() {
	for (var i=0; i<numberOfMenuLevels; i++) {
		ifrm = document.createElement("IFRAME");
		ifrm.setAttribute('id', 'menushim' + i);
		ifrm.setAttribute('class', 'menushim');
		ifrm.setAttribute('src', sIframeShimUrl);
		ifrm.setAttribute('scrolling', 'no');
		ifrm.setAttribute('frameborder', '0');
		document.body.appendChild(ifrm);
		ifrm2 = document.getElementById('menushim' + i);
		ifrm2.className = 'menushim';
	}
}


sfHover = function() {
	if (document.getElementById('nav') == null) return;
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		/* Mark Expandable Menu Items */
		sfMarkContainer = sfEls[i].getElementsByTagName("LI");
		if(sfMarkContainer.length > 0) {
			topNavAddClassName("sfexpandable", sfEls[i]);

			/*
			sfHover Function applies ONMOUSEOVER and ONMOUSEOUT states to each LI (if it has subordinate levels) in the UL (id="nav)".
			*/
			if (window.attachEvent) { //ie
				/* Mouseover */
				sfEls[i].onmouseover=function() {
					
					topNavAddClassName("sfhover", this);
					tmpobj = this.getElementsByTagName('ul');
		
					if(this.parentNode.id == "nav"){ //When LI tag (in 1st Level) is moused over, place iframe behind exposed 2nd Level
						mnushm = document.getElementById('menushim0');
						setIFrame(tmpobj[0], mnushm, "0");
						tmpobj[0].style.zIndex="101";
					} else if(this.parentNode.parentNode.parentNode.id == "nav") { //When LI tag (in 2st Level) is moused over, place iframe behind exposed 3rd Level
						mnushm2ndLevel = document.getElementById('menushim1');
						setIFrame(tmpobj[0], mnushm2ndLevel, "1");
						tmpobj[0].style.zIndex="102";
					}
				}
				sfEls[i].onmouseout=function() {
					this.className=this.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
					tmpobj = this.getElementsByTagName('ul');
					if (tmpobj[0] != undefined) {
						if (this.parentNode.id == "nav") { //When LI tag (in 1st Level) is moused out, hide iframe for 2nd Level
							mnushm = document.getElementById('menushim0');
							mnushm.style.display="none";
						} else if(this.parentNode.parentNode.parentNode.id == "nav") { //When LI tag (in 2nd Level) is moused out, hide iframe for 3nd Level
							mnushm2ndLevel = document.getElementById('menushim1');
							mnushm2ndLevel.style.display="none";
						}
					}
				}
			}
		}
	}
}

/*
mcAccessible Function applies ONFOCUS and ONBLUR states to each A (if its parent LI has subordinate levels) in the UL (id="nav)".
*/
mcAccessible = function() {
	
	if (document.getElementById('nav') == null) return;
	var mcEls = document.getElementById("nav").getElementsByTagName("A");
	for (var i=0; i<mcEls.length; i++) {
		// ONFOCUS Actions applied to A Tags if Subordinate Menus exist
		mcEls[i].onfocus=function() {
			keyboardFocus = this;
			topNavAddClassName("sffocus", this);
			topNavAddClassName("sfhover", this.parentNode);
			
			if(this.parentNode.parentNode.parentNode.nodeName == "LI") {
				topNavAddClassName("sfhover", this.parentNode.parentNode.parentNode);
				
				if(this.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "LI") {
					topNavAddClassName("sfhover", this.parentNode.parentNode.parentNode.parentNode.parentNode);
				}
			}
			if (window.attachEvent) { //ie
				// IFRAME fix
				var tmpobj = this.parentNode.getElementsByTagName('ul');
				
				if(tmpobj[0] != undefined){
					if(this.parentNode.parentNode.id == "nav"){  //When A tag (in 1st Level) is in Focus Place iframe behind exposed 2nd Level
						var mnushm = document.getElementById('menushim0');
						
						setIFrame(tmpobj[0], mnushm, "0");
						tmpobj[0].style.zIndex="101";
	
					} else if(this.parentNode.parentNode.parentNode.parentNode.id == "nav") {  //When A tag (in 2nd Level) is in Focus Place iframe behind exposed 3rd Level
						var tmpobj2 = tmpobj[0].parentNode.parentNode;
						var mnushm = document.getElementById('menushim0');
						var mnushm2ndLevel = document.getElementById('menushim1');
						setIFrame(tmpobj[0], mnushm, "0");
						setIFrame(tmpobj2, mnushm2ndLevel, "1");
						tmpobj[0].style.zIndex="102";
					} 
				} else {
					if (this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.id == "nav") {
						tmpobj = this.parentNode.parentNode;
						var tmpobj2 = tmpobj.parentNode.parentNode;
						var mnushm = document.getElementById('menushim0');
						var mnushm2ndLevel = document.getElementById('menushim1');
						setIFrame(tmpobj, mnushm, "0");
						setIFrame(tmpobj2, mnushm2ndLevel, "1");
						tmpobj2.style.zIndex="101";
						tmpobj.style.zIndex="102";
					} else if (this.parentNode.parentNode.parentNode.parentNode.id == "nav") {
						tmpobj = this.parentNode.parentNode;
						var mnushm = document.getElementById('menushim0');
						setIFrame(tmpobj, mnushm, "0");;
						tmpobj.style.zIndex="101";
					} else if (this.parentNode.parentNode.id == "nav") {
						var mnushm = document.getElementById('menushim0');
						mnushm.style.display="none";
						mnushm2ndLevel = document.getElementById('menushim1');
						mnushm2ndLevel.style.display="none";
					}
				}
			}
		}
		// ONBLUR Actions applied to A Tags if Subordinate Menus exist
		mcEls[i].onblur=function() {
			keyboardFocus = null;
			this.className=this.className.replace(new RegExp("( ?|^)sffocus\\b"), "");
			this.parentNode.className=this.parentNode.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
			if (window.attachEvent) { //ie
				var mnushm = document.getElementById('menushim0');
				if (mnushm != null) mnushm.style.display="none";
				var mnushm2ndLevel = document.getElementById('menushim1');
				if (mnushm2ndLevel != null) mnushm2ndLevel.style.display="none";
			}
			if(this.parentNode.parentNode.parentNode.nodeName == "LI") {
				this.parentNode.parentNode.parentNode.className=this.parentNode.parentNode.parentNode.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
				if(this.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "LI") {
					this.parentNode.parentNode.parentNode.parentNode.parentNode.className=this.parentNode.parentNode.parentNode.parentNode.parentNode.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
					
				}
			}
			
			
		}
		mcEls[i].onmouseup=function() {
			this.blur();
		}
	}
}

sfCollision = function() {
	if (document.getElementById('nav') == null) return;
	var sfElsDet = document.getElementById('nav').getElementsByTagName('UL');
	var browserWindowWidth = topNavWindowEdges().right;
	//alert(browserWindowWidth + ";" + thewidth.right);
	for (var i=0; i<sfElsDet.length; i++) {
		sfElsDet[i].className = "";
	}
	for (var i=0; i<sfElsDet.length; i++) {
		if(sfElsDet[i].parentNode.parentNode.id == "nav") {
			var objCoordinatesPassed = topNavElementCoordinates(sfElsDet[i].parentNode);
			var colParentLeft = objCoordinatesPassed.x;
			var colThisWidth = sfElsDet[i].offsetWidth;
			if(browserWindowWidth < colThisWidth + colParentLeft) {
				sfElsDet[i].className = "sfCollision";
			}
		} else if ((sfElsDet[i].parentNode.parentNode.parentNode.parentNode.id == "nav")&&(sfElsDet[i].parentNode.parentNode.className != "sfCollision")) {
			var objCoordinatesPassed = topNavElementCoordinates(sfElsDet[i].parentNode.parentNode.parentNode);
			var colParentParentLeft = objCoordinatesPassed.x;
			//var colParentParentLeft = sfElsDet[i].parentNode.parentNode.parentNode.offsetLeft;
			var colParentWidth = sfElsDet[i].parentNode.offsetWidth;
			var colThisWidth = sfElsDet[i].offsetWidth;
			if(browserWindowWidth < colThisWidth + colParentWidth + colParentParentLeft) {
				sfElsDet[i].className = "sfCollision";
			}
		}
	}
}

function setIFrame(theElement, iFrameShim, theZIndex) {
	var coords = topNavElementCoordinates(theElement);
	//alert(coords.x + ";" + coords.y);
	var dimens = topNavElementDimensions(theElement);
	iFrameShim.style.left = coords.x + 'px';
	iFrameShim.style.top = coords.y + 'px';
	iFrameShim.style.height = dimens.h + 'px';
	iFrameShim.style.width = dimens.w + 'px';
	iFrameShim.style.zIndex = theZIndex;
	iFrameShim.style.display = 'block';
	
}
/* THESE FUNCTIONS SHOULD BE REPLACED BY COMMON UTILITY FUNCTIONS */
// Retrieves the exact positioning of the requested drop down menu
function topNavElementCoordinates(theElement) {
	var elementXpos = 0;
	var elementYpos = 0;
	for ( e = theElement; e; e = e.offsetParent) {
		//alert(e.offsetLeft + ";" + e.tagName + ";" + e.className + ";" + elementXpos);
		elementXpos += e.offsetLeft;
		elementYpos += e.offsetTop;
	}

		for ( e = theElement.parentNode; e && e != document.body; e = e.parentNode) {
			if (e.scrollTop > 0) alert(e.scrollTop + ";" + e.tagName + ";" + e.className + ";" + elementYpos);
			if (e.scrollTop) elementYpos -= e.scrollTop;
			if (e.scrollLeft) elementXpos -= e.scrollLeft;
		}

	var elementCoordinates = new Object;
	elementCoordinates.x = elementXpos;
	elementCoordinates.y = elementYpos;
	return elementCoordinates;
}
function topNavElementDimensions(theElement) {
	var elementHeight = theElement.offsetHeight;
	//alert("h:" + elementHeight);
	var elementWidth = theElement.offsetWidth;
	//alert("w:" + elementWidth);
	var elementDimensions = new Object;
	elementDimensions.h = elementHeight;
	elementDimensions.w = elementWidth;
	return elementDimensions;
}

function topNavWindowEdges() {
	var topEdge = 0;
	var bottomEdge = 0;
	var rightEdge = 0;
	var leftEdge = 0;
	if (isIE) {
		topEdge = document.documentElement.scrollTop;
		bottomEdge = document.documentElement.scrollTop + document.documentElement.clientHeight;
		leftEdge = document.documentElement.scrollLeft;
		rightEdge = document.documentElement.scrollLeft + document.documentElement.clientWidth;
	} else {
		topEdge = window.pageYOffset;
		bottomEdge = window.pageYOffset + window.innerHeight;
		leftEdge = window.pageXOffset;
		rightEdge = window.pageXOffset + window.innerWidth;
	}
	//alert(topEdge + ";" + bottomEdge + ";" + rightEdge + ";" + leftEdge);
	var windowEdges = new Object;
	windowEdges.top = topEdge;
	windowEdges.bottom = bottomEdge;
	windowEdges.right = rightEdge;
	windowEdges.left = leftEdge;
	return windowEdges;
}
function topNavHasClassName(theClass, theNode) {
	var begpattern = new RegExp("^" + theClass + "([ ])");
	var endpattern = new RegExp("[ ]" + theClass + "$");
	if (theNode && theNode.className) {
		return ((theNode.className==theClass)||(theNode.className.search(endpattern)!=-1)||(theNode.className.search(" " + theClass + " ")!=-1)||(theNode.className.search(begpattern)!=-1)); 
	} else {
		return false;
	}
}
function topNavAddClassName(theClass, theElement) {
	//alert(theElement.tagName);
	if (theElement) {
		if (!topNavHasClassName(theClass, theElement)) {
			if (theElement.className.length > 0) {
				theElement.className += " " + theClass;
			} else {
				theElement.className = theClass;
			} 
		}
	}
}
/* END REPLACE */

if(window.addEventListener) {  // gecko, safari, opera, konqueror and standard
	window.addEventListener('load', mcAccessible, false);
	window.addEventListener('load', sfHover, false);
	window.addEventListener('load', sfCollision, false);
	window.addEventListener('resize', sfCollision, false);
	
}else if(window.attachEvent) { // win/ie
	window.attachEvent('onload', sfHover);
	window.attachEvent('onload', mcAccessible);
	window.attachEvent('onload', makeFrame);
	window.attachEvent('onload', sfCollision);
	window.attachEvent('onresize', sfCollision);
	
} else { // mac/ie5
	if(typeof window.onload == 'function') {
		var existing = onload;
		var existingr = onresize;
		window.onload = function() {
			existing();
			sfHover();
			mcAccessible();
			sfCollision();
		}
		window.onresize = function() {
			existingr();
			sfCollision();
		}
	} else {
		window.onload = function() {
			sfHover();
			mcAccessible();
			sfCollision();
		}
		window.onresize = function() {
			sfCollision();
		}
	}
}



// Check if sIframeShimUrl has been declared and a valid value has been assign 
// to it in file app_config_for_presentation_fw.js. 

if (typeof sIframeShimUrl == "undefined")  {
    alert('JavaScript variable "sIframeShimUrl " has not been declared ' +
        'or no value has been assigned to it. Please make sure that file ' +
        'app_config_for_presentation_fw.js (distributed with the Presentation '+
        'framework) has been included in your JSP page and that it contains ' +
        'a definition for variable "sIframeShimUrl ".');
}
  
if(typeof sIframeShimUrl != "undefined"){
    if(sIframeShimUrl == null || sIframeShimUrl == "" ){
      alert('JavaSript variable "sIframeShimUrl" in file ' +
      'app_config_for_presentation_fw.js contains an invalid value (empty ' +
      'String or null). Please assign a valid value to "sIframeShimUrl".');
    }
}